package com.chnbst.bstyun.controller.admin;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.GoodsType;
import com.chnbst.bstyun.model.vo.GoodsTypeVo;
import com.chnbst.bstyun.service.GoodsRelationService;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.GoodsTypeService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author WangXinLei
 *
 */
@Controller
@RequestMapping("/sysAdmin")
public class GoodsTypeAdminController {
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private GoodsTypeService goodsTypeService;
	@Autowired 
	private GoodsService goodsService;
	@Autowired
	private GoodsRelationService goodsRelationService;
	@Autowired
	private Eos eos;
	
	/**
	 * 商品分类界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goodsTypeAdmin/goodsSort",method=RequestMethod.GET)
	public ModelAndView goodsSort()throws Exception{
		
		List<GoodsType> goodsTypeList = goodsTypeService.selectGoodsType();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("goodsTypeList",goodsTypeList);
		mav.setViewName("/admin/productManager/goodsSort");
		
		return mav;
	}
	
	/**
	 * @return 查询所有的商品类型作为导航树所用
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/goodsTypeAdmin/selectGoodsType",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectGoodsType(HttpServletRequest request){
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<GoodsType> goodsTypeList = goodsTypeService.selectGoodsType();
		
		map.put("goodsTypeList",goodsTypeList);
		return map;
	}
	
	/**
	 * @return 分页查询所有的商品类型作为列表(可传入条件 typeId，typeName)
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/goodsTypeAdmin/selectGoodsTypeGrid",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectGridByCondition(String typeId,HttpServletRequest request)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(!"".equals(typeId))
			map.put("typeId", typeId);
		
		//查询商品类别列表
		List<GoodsTypeVo> goodsTypeList = goodsTypeService.selectGridByCondition(map);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("goodsTypeList", goodsTypeList);
		
		return resultMap;
		
	}
	
	/**
	 * @return 新增商品类型
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/goodsTypeAdmin/saveGoodsType",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveGoodsType(String id,String parentid,Integer no,String name,Integer level,
			String subpro,String subname,String note,HttpServletRequest request)throws Exception{
		boolean isSuccess = false;
		
		GoodsType goodsType = new GoodsType();
		goodsType.setTypeId(id);
		goodsType.setParentId(parentid);
		goodsType.setTypeName(name);
		goodsType.setTypeNum(no);
		goodsType.setTypeLevel(level);
		goodsType.setCreateTime(new Date());
		goodsType.setIsDelete(0);
		goodsType.setRemark(note);
		
		if(goodsType.getTypeId() == null){
			isSuccess = goodsTypeService.insertGoodsType(goodsType);
		}else{
			isSuccess = goodsTypeService.updateGoodsType(goodsType);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		
		return map;
		
	}
	
	/**
	 * @return 查询单条商品类型
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/goodsTypeAdmin/selectGoodsTypeInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectGoodsTypeInfo(String id,HttpServletRequest request)throws Exception{
		GoodsTypeVo goodsTypeVo = goodsTypeService.selectGoodsTypeVoInfo(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goodsType", goodsTypeVo);
		return map;
		
	}
	
	/**
	 * @return 根据ID 删除商品类别
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/goodsTypeAdmin/deleteByTypeId",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteByTypeId(String ids,HttpServletRequest request)throws Exception{
		
		boolean isDelete = true;
		String isDeleteIds = "";
		String[] goodsTypeList = ids.split(",");
		for(int i=0;i<goodsTypeList.length;i++){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("typeId", goodsTypeList[i]);
			List<GoodsTypeVo> gtList = goodsTypeService.selectGridByCondition(map);
			if(gtList.size() > 0){
				isDelete = false;
			}else{
				List<Goods> goodsList = goodsService.selectGoodsByTId(goodsTypeList[i]);
				if(goodsList.size() > 0){
					isDelete = false;
				}else{
					GoodsType goodsType = goodsTypeService.selectGoodsTypeVoInfo(goodsTypeList[i]);
					goodsType.setIsDelete(1);
					goodsTypeService.updateGoodsType(goodsType);
					if(isDeleteIds.equals("")){
						isDeleteIds = goodsTypeList[i];
					}else{
						isDeleteIds = isDeleteIds + "," + isDeleteIds;
					}
				}
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isDelete", isDelete);
		map.put("isDeleteIds", isDeleteIds);
		
		return map;
		
	}
}
